#######################################################################################################################
# File Name  : Makefile.static                                                                                        #
# Description: This makefile contains the platform/compiler specific static settings (will be included by Makefile)   #
# Project    : Vector Basic Runtime System                                                                            #
# Module     : BrsHw for Platform Renesas RH850                                                                       #
#              and Compiler GreenHills,                                                                               #
#              using Vector PES Makesupport 3.13                                                                      #
#                                                                                                                     #
#---------------------------------------------------------------------------------------------------------------------#
# COPYRIGHT                                                                                                           #
#---------------------------------------------------------------------------------------------------------------------#
# Copyright (c) 2017 by Vector Informatik GmbH.                                                  All rights reserved. #
#                                                                                                                     #
#---------------------------------------------------------------------------------------------------------------------#
# AUTHOR IDENTITY                                                                                                     #
#---------------------------------------------------------------------------------------------------------------------#
# Name                          Initials      Company                                                                 #
# ----------------------------  ------------  ------------------------------------------------------------------------#
# Sherif Elsabbahy              vissey        Vector Informatik GmbH                                                  #
# Sascha Mauser                 vismaa        Vector Informatik GmbH                                                  #
#---------------------------------------------------------------------------------------------------------------------#
# REVISION HISTORY                                                                                                    #
#---------------------------------------------------------------------------------------------------------------------#
# Version   Date        Author  Description                                                                           #
# --------  ----------  ------  --------------------------------------------------------------------------------------#
# 01.00.00  2017-05-04  vissey  Initial creation (splitted out of Makefile) for Brs_Template 2.01.00                  #
# 01.00.01  2017-05-05  vismaa  Fixed hex target                                                                      #
#######################################################################################################################

#------------------------------------------------------------------------------
# Include the project specific settings
#------------------------------------------------------------------------------
include Makefile.config
include Makefile.derivative.settings
include Makefile.project.part.defines

#------------------------------------------------------------------------------
# Version of MakeSupport used (current version is 3)
#------------------------------------------------------------------------------
VERSION     = 3
SUB_VERSION = 13

ifeq ($(OS_USECASE),)
#Default is BRS
OS_USECASE = BRS
endif

###############################################################################
# Platform/compiler/derivative/emulator are relevant for the include of       #
# linker and symbol preprocessor command file identified by name.             #
# Vector standard names have to be used                                       #
###############################################################################
#------------------------------------------------------------------------------
# Compiler Manufacturer used in this project
# E.g.: ARM, COSMIC, DIABDATA, MICROTEC, TASKING, FUJITSU, GHS, NEC, GNU, GAIO
#       HIWARE, IAR, KEIL, MITSUBISHI, OKI, HEW, TI, GHS, TOSHIBA, DIABDATA,
#       RENESAS
#       ALL is special for "all available compilers are supported in this file"
#------------------------------------------------------------------------------
COMPILER_MANUFACTURER = GHS

#------------------------------------------------------------------------------
# Platform used for this project
# E.g.: 78K0, AT91, C16X, C5x5, CANOE, CR16, FJ16LX, H8S, HC08, HC12, M16C
#       M32C, M32R, MC68376, MGT5100, MPC, MSM9225, PIC, SH2, ST7, TLC900
#       TMS470, V850-DCAN, V850X, XC16X
#------------------------------------------------------------------------------
PLATFORM = RH850

#------------------------------------------------------------------------------
# Emulator used for this project
# E.g.: HITEX, LAUTERBACH, ISYSTEMS, GHS
#       ALL is special for "all derivatives are supported in this file"
#------------------------------------------------------------------------------
EMULATOR = GHS

#------------------------------------------------------------------------------
# $(OBJ_OUTPUT) defines the way, how to generate error files
# It should be one of:
#     MOVE  - Object files are generated by compiler/linker and will be moved
#             to $(OBJ_PATH) after generation by external tool
#     FLAGS - Object file path definition is supported by compiler.
#             Therefore the $(CFLAGS) have to be adjusted.
#------------------------------------------------------------------------------
OBJ_OUTPUT = FLAGS

#------------------------------------------------------------------------------
# $(ERR_OUTPUT) defines the way, how to generate error files
# It should be one of:
#     MOVE  - Error files are generated by compiler/linker and will be moved
#             to $(ERR_PATH) after generation by external tool
#     PIPE  - Error will be printed to sterr/stdout and redirected to
#             $(ERR_PATH)
#     FLAGS - Error file generation with path definition is supported by
#             compiler. Therefore e.g. the $(CFLAGS) have to be adjusted.
#------------------------------------------------------------------------------
ERR_OUTPUT = FLAGS

#------------------------------------------------------------------------------
# $(LIB_OUTPUT) defines the way, how to generate lib files
# It should be one of:
#     MOVE  - Lib files are generated by compiler/linker and will be moved
#             to $(LIB_PATH) after generation.
#     FLAGS - Object file path definition is supported by compiler.
#             Therefore the $(LB_FLAGS) have to be adjusted.
#------------------------------------------------------------------------------
LIB_OUTPUT = FLAGS

#------------------------------------------------------------------------------
#------------------------- MUST be filled out ---------------------------------
# Suffix of generated object files. Generated objects are then *.$(OBJ_SUFFIX)
#------------------------------------------------------------------------------
OBJ_SUFFIX = o

#------------------------------------------------------------------------------
#------------------------- MUST be filled out ---------------------------------
# Suffix of assembler files.
#------------------------------------------------------------------------------
ASM_SUFFIX = asm

#------------------------------------------------------------------------------
#------------------------- MUST be filled out ---------------------------------
# Suffix of generated list files. Generated list files are then *.$(LST_SUFFIX)
#------------------------------------------------------------------------------
LST_SUFFIX = lst

#------------------------------------------------------------------------------
#------------------------- MUST be filled out ---------------------------------
# Suffix linker command file.
#------------------------------------------------------------------------------
LNK_SUFFIX = ld

#------------------------------------------------------------------------------
#------------------------- MUST be filled out ---------------------------------
# Suffix binary output file.
#------------------------------------------------------------------------------
BINARY_SUFFIX = elf

#------------------------------------------------------------------------------
#------------------------- MUST be filled out ---------------------------------
# Suffix of generated library files. Generated objects are then *.$(LIB_SUFFIX)
#------------------------------------------------------------------------------
LIB_SUFFIX = lib

#------------------------------------------------------------------------------
# Suffixes of generated error files which are placed in $(ERR_PATH)
# (necessary for Visual Studio / Codewright project file generation)
# Default (nothing entered) is err
# E.g. ERR_SUFFIX_LIST = err
#------------------------------------------------------------------------------
ERR_SUFFIX_LIST = err

#------------------------------------------------------------------------------
# Suffixes of generated asm/list files which are placed in $(LST_PATH)
# (necessary for Visual Studio / Codewright project file generation)
# Default (nothing entered) is lst
# E.g. LST_SUFFIX_LIST = lst asm
#------------------------------------------------------------------------------
LST_SUFFIX_LIST = lst

#------------------------------------------------------------------------------
# The following define overrides the rule - how to assemble a file -
# implemented in Global.Makefile.target.make.
#------------------------------------------------------------------------------
#override define ASSEMBLE_RULE
#  $(ECHO) "Overriden AssembleRule assembling : $<"; \
#  $(AS) $(ASFLAGS) $(INCLUDES) $< $(REDIRECT_OUTPUT);
#endef

#------------------------------------------------------------------------------
# The following define overrides the rule - how to compile a file -
# implemented in Global.Makefile.target.make.
#------------------------------------------------------------------------------
override define COMPILE_RULE
  if [ -n "$(MKVERBOSE)" ] ; then \
    $(ECHO) "    $(CC) $(filter-out $(FILTER_$(call _toUpper,$*)),$(CFLAGS)) $(CFLAGS_$(call _toUpper,$*)) $(INCLUDES) $(subst /,\,$<) $(REDIRECT_OUTPUT)"; \
  fi; \
  $(ECHO) > $(ERR_PATH)/$*.err; \
  $(CC_ENV) \
  $(subst \,/,$(CC) $(filter-out $(FILTER_$(call _toUpper,$*)),$(CFLAGS)) $(CFLAGS_$(call _toUpper,$*))) $(INCLUDES_UNIX) $< $(REDIRECT_OUTPUT);
endef

#------------------------------------------------------------------------------
# The following define overrides the rule - how to link the object files -
# implemented in Global.Makefile.target.make.
#------------------------------------------------------------------------------
ifeq ($(OS_USECASE),SAFECTX)
override define LINK_RULE
  if [ -n "$(MKVERBOSE)" ] ; then                                 \
    $(ECHO) "    $(LD) $(OBJECTS) $(LDFLAGS)";                    \
  fi;                                                             \
  if [ -n "$(USER_LINKER_COMMAND_FILE)" ] ; then                  \
    $(ECHO) "Linking with $(USER_LINKER_COMMAND_FILE)...";        \
  fi;                                                             \
  $(LD_ENV)                                                       \
  $(subst \,/,$(LD) $(OBJECTS) $(LDFLAGS)) $(REDIRECT_OUTPUT);    \
  if [ -f $(PROJECT_NAME).elf ] ; then                            \
  $(ECHO) "Patching osConfigBlock CRC in $(PROJECT_NAME).elf..."; \
  if [ -n "$(MKVERBOSE)" ] ; then                                 \
    $(ECHO) " $(subst \,\\,$(ELFCONVERTER_BIN)) $(PROJECT_NAME).elf _osConfigBlock --patch_crc --out CRCChecksum.hex"; \
  fi;                                                             \
  $(subst \,\\,$(ELFCONVERTER_BIN)) $(PROJECT_NAME).elf _osConfigBlock --patch_crc --out CRCChecksum.hex;            \
        fi;
endef
else
override define LINK_RULE
  if [ -n "$(MKVERBOSE)" ] ; then \
    $(ECHO) "    $(LD) $(OBJECTS) $(LDFLAGS)"; \
  fi; \
  if [ -n "$(USER_LINKER_COMMAND_FILE)" ] ; then                  \
    $(ECHO) "Linking with $(USER_LINKER_COMMAND_FILE)...";        \
  fi;                                                             \
  $(LD_ENV) \
  $(subst \,/,$(LD) $(OBJECTS) $(LDFLAGS)) $(REDIRECT_OUTPUT);
endef
endif

#------------------------------------------------------------------------------
# The following define overrides the rule - how to build a lib file -
# implemented in Global.Makefile.target.make.
#------------------------------------------------------------------------------
#override define LIB_RULE
#  if [ -n "$(MKVERBOSE)" ] ; then \
#    $(ECHO) '    $(LB_U) $(LBFLAGS) $(subst \,/,$^) $(REDIRECT_OUTPUT)'; \
#  fi; \
#  $(LB_ENV_U) $(LB_U) $(subst \,/,$^) $(LBFLAGS) $(REDIRECT_OUTPUT);
#endef

override define MOVE_OBJECT_RULE
  $(ECHO) "Moving object file: $*.$(OBJ_SUFFIX) -> $(OBJ_PATH)/$*.$(OBJ_SUFFIX)"; \
  if [ -f $*.$(OBJ_SUFFIX) ]; then $(MV) $*.$(OBJ_SUFFIX) $(OBJ_PATH)/$*.$(OBJ_SUFFIX); fi;
endef

#------------------------------------------------------------------------------
# make builds the whole project by default
# To make another target you have to add it to the prerequisites of default.
# E.g.: default: all $(PROJECT_NAME).opt
#------------------------------------------------------------------------------
default: all hex

#------------------------------------------------------------------------------
# If platform specific rules exist, they can be described here to be shown
# in the usage output of the help target
# E.g.: PLATFORM_HELP = \
#           $(ECHO) "m mytarget ................ -- build mytarget"; \
#           $(ECHO) "                               nextline";
#------------------------------------------------------------------------------
PLATFORM_HELP = $(ECHO) "m multi ................... -- start Multi 2000 debugger";

#------------------------------------------------------------------------------
# List of (regexp) pattern used by the clean target.
# Deleted by default are:
#    $(PROJECT_NAME).$(LNK_SUFFIX)
#    $(PROJECT_NAME).dep
#    $(PROJECT_NAME).hex
#    $(PROJECT_NAME).ilk
#    $(OBJ_PATH)\*.*
#    $(ERR_PATH)\*.*
#    $(LST_PATH)\*.*
#    $(LIB_PATH)\*.*
#    $(LOG_PATH)\*.*
#------------------------------------------------------------------------------
MAKE_CLEAN_PATTERN_LIST = $(PROJECT_NAME).rc $(PROJECT_NAME).dla $(PROJECT_NAME).dnm $(PROJECT_NAME).elf.bld

#------------------------------------------------------------------------------
# List of (regexp) pattern used by the distclean target.
# Deleted by default are:
#    ~vssc
#    vssver.scc
#    *~
#    *.*~
#    *.~*
#    *.bak
#    *.log
#    $(GENDATA_DIR)\*.txt
#    $(ERR_LIST_WOP)
#    $(PROJECT_NAME).map
#    $(PROJECT_NAME).dep
#    $(PROJECT_NAME).dsw
#    $(PROJECT_NAME).dsp
#    $(PROJECT_NAME).sln
#    $(PROJECT_NAME).vcproj
#    $(PROJECT_NAME).suo
#    $(PROJECT_NAME).ncb
#    $(PROJECT_NAME).opt
#    $(PROJECT_NAME).plg
#    $(PROJECT_NAME).doc.cfg
#    $(GENDATA_DIR)\BrsVInfo.h
#------------------------------------------------------------------------------
DIST_CLEAN_PATTERN_LIST = *.dla *.dln *.dnm *.sbl *.elf.bld *.rc *.LST

###############################################################################
# resource information generation
###############################################################################

#------------------------------------------------------------------------------
# Select the source, which contains the memory information
# E.g.: RESOURCE_FILE = LIST
#       RESOURCE_FILE = MAP
#       RESOURCE_FILE = NONE
#------------------------------------------------------------------------------
RESOURCE_FILE = MAP

#------------------------------------------------------------------------------
# The extension of the map or list file.
# E.g.: RESOURCE_FILE_EXT = map
#       RESOURCE_FILE_EXT = lst
#------------------------------------------------------------------------------
RESOURCE_FILE_EXT = map

#------------------------------------------------------------------------------
# This is the main resource script written in the language AWK.
# For more information about AWK see the following resources:
#   http://torvalds.cs.mtsu.edu/~neal/awkcard.pdf (recommendable)
#   http://www.uni-magdeburg.de/urzs/awk/
#   http://de.wikipedia.org/wiki/AWK
# Note: The character '$' must be escaped with a second '$'. So write '$$'.
#       Also the character '#' must be escaped using a backslash -> \#.
# For a more detailed description about what is going on in this script look
# at the file:  fetch-memory-info-template-documented.awk
# In order to see what the ressource scanner is doing it is highly recommended
# to set the environment variable GAWK_DEBUG=1. This option will generate a
# further file ($(PROJECT_NAME)_resource_debug.txt) in your log directory.
# Here is a short description about the instructions which has to be configured:
# RS means Record Separator and is set to a char which is used to split the
#   files into records. For each record the processing instructions are done.
#   The record string is assigned to the variable $0 (remember to access via $$0).
# FS means Field Separator and splits each record into fields. These fields
#   are referenced from $1, $2... $NF, where NF is the number of fields.
#   If FIELDWIDTHS is set, the value of FS is ignored.
# FIELDWIDTHS is a space separated list, each field is expected to have a fixed
#   width. The value of FS is ignored.
# IGNORECASE is a bool variable. When set to 1 comparsions are made case-
#   insensitive in comparsions with '==' and regular expressions.
# memoryTable maps the second argument of addEntry() to the according section.
# startProcessing() and stopProcessing() implements a little restriction
#   to which section of the file the instructions are executed.
# addEntry(module, memory, size, isHex) is the main API function you have to
#   use. It adds an entry to the internal array. You specify with the param.
#   how many bytes (param:size) to which module (param:module) and to which
#   type of memory (param:memory) you want to add. If you set the param
#   isHex to 1 the param size is treated as being hexadecimal.
# Other API functions are: trim(str), getStringWithinParentheses(str),
#   getStringWithinBrackets(str)
#------------------------------------------------------------------------------
RESOURCE_FETCH_SCRIPT = BEGIN {                                     \
  RS  = "\n";                                                       \
  FS  = "[ \\+]+";                                                  \
  IGNORECASE = 1;                                                   \
  memoryTable["ram"]    = "bss|FLASHDRV|FBLMAGIC";                  \
  memoryTable["iram"]   = "data|RAMCODE";                           \
  memoryTable["const"]  = "rodata|rozdata|rosdata|romdata|intvect|FBLHEADER"; \
  memoryTable["code"]   = "text|ROM.RAMCODE";                       \
  memoryTable["ignore"] = "debug|syscall|fixtype|fixaddr|COMMON|APPLVECT|note.renesas|linfix|secinfo"; \
  memoryTableOrder      = "const,code,ram,iram,ignore";             \
}                                                                   \
                                                                    \
/^Module Summary/    { startProcessing() }                          \
/^Global Symbols/    { stopProcessing() }                           \
                     { process() }                                  \
                                                                    \
$$2 ~ /^[0-9a-f]+$$/ {                                              \
  addEntry($$NF, $$3, $$2, 1);                                      \
  if ($$3 == ".RAMCODE") {                                          \
    addEntry($$NF, "ROM.RAMCODE", $$2, 1);                          \
  }                                                                 \
  if (($$3 != ".RAMCODE") && ($$5 == ".ramcopy")) {                 \
    addEntry($$NF, "RAMCODE", $$2, 1);                              \
  }                                                                 \
}

#------------------------------------------------------------------------------
# Check if all necessary variables are set
#------------------------------------------------------------------------------
ifeq ($(MAKESUPPORT_DIR),)
  $(error MAKESUPPORT_DIR not set in m.bat or m.bat not called)
endif

ifeq ($(PLATFORM),)
  $(error Variable PLATFORM is not defined)
endif

ifeq ($(COMPILER_MANUFACTURER),)
  $(error Variable COMPILER_MANUFACTURER is not defined)
endif

ifeq ($(EMULATOR),)
  $(error Variable EMULATOR is not defined)
endif

ifeq ($(VERSION),)
  $(error Variable VERSION is not defined)
endif

ifeq ($(MAKESUPPORT_DIR),)
  $(error Variable MAKESUPPORT_DIR is not defined)
endif

ifeq ($(GHS_CPU_SWITCH_$(DERIVATIVE_GROUP_$(DERIVATIVE))),)
  $(error "Value for compiler switch -cpu is not yet defined for your derivative group within Makefile.derivative.settings.")
endif

#------------------------------------------------------------------------------
# List of variables in Makefile.config which are required and must be set by
# user. Check is performed by global makefile
#------------------------------------------------------------------------------
REQUIRED_MK_CONF_VARS = DERIVATIVE     \
                        MAIN_OSC_CLK   \
                        TIMEBASE_CLOCK \
                        DEBUGGER       \
                        DEVICE_FILE_NAME

#End of Makefile.static
